//index.js
const app = getApp() 

Page({
  data: {
    "love": "/resources/images/cardsPage/lovefinal.png",
    nowsort:0,
    //侧边栏变量
    left:false,
    //侧边栏内容变量
    active1: [0],
    title1: '距离',
    title2: '分类',
    title3: '发表时间',
    //内容变量
    array3: [0, 1, 2],
    //tab vars
    tabs2: ['近', '热'],
    tabsWithName: [
      { name: 'a', index: 1 },
      { name: 'b', index: 2 },
      { name: 'c', index: 3 }
    ]
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    // wx.request({
    //   url: app.globalData.urlHome + '/task/finishTask',
    //   header: {
    //     "accept": "*/*",
    //     "content-type": "application/json",
    //     "Authorization": app.globalData.token
    //   },
    //   data: {
    //     "openid": app.globalData.openid,
    //     "type": 9
    //   },
    //   method: 'POST',
    //   success(r) {
    //     console.log(r.data);
    //   },
    //   fail(e) {
    //   }
    // })
  },
  onLoad(){
    var that = this  
    wx.showLoading({
      title: '加载中...',
    })  
    wx.request({  
      url: app.globalData.urlHome+'/card/getCards',
      header:{
        "accept": "*/*",
        "content-type": "application/json",
        "Authorization":app.globalData.token
      },
      data:{
        "latitude":app.globalData.latitude,
        "longitude":app.globalData.longitude,
        "distance":20000000
      },
      method:'POST',
      success(res){
        var len=Object.keys(res.data).length;
        var tem=[];
        var origin=[];
        for(var i=1;i<=len;i++){
          var key = "key" + i.toString();
          var card=res.data[key];
          card.id=i;
          tem.push(card);
          origin.push(card);
        }
        console.log(tem);
        that.setData({
          pics:tem,
          originPics:origin
        })
        that.disSort();
        // that.hotSort();
        //that.taskFinish();
        wx.hideLoading({
          success: (res) => {},
        });
      },
      fail(e){
        wx.showModal({
          title: '请登陆获取token！',
        })
      }
    })
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
  },
  Encoder(str) {
    var res = "";
    for (var i = 0; i < str.length; i++) {
      if (str[i] == '&') {
        res += '#';
      } else {
        res += str[i];
      }
    }
    return res;
  },
  ClickedOne(e){
    var that = this;
    var index = e.currentTarget.dataset['index']
    wx.navigateTo({
      url: '../detail/detail?json=' + that.Encoder(JSON.stringify(that.data.originPics[index - 1])),
    })
  },  
  //侧边栏弹出函数
  toggle(type, show) {
    this.setData({
      [`show.${type}`]: show
    });
  },
  showRight() {
    this.toggle('right', true);
  },
  hideRight() {
    this.toggle('right', false);
  },
  //下拉栏函数
  onChange(event) {
    const { key } = event.currentTarget.dataset;
    this.setData({
      [key]: event.detail,
    });
  },
  onOpen(event) {
    Toast(`展开: ${event.detail}`);
  },
  onClose(event) {
    Toast(`关闭: ${event.detail}`);
  },
  //tab function
  onClickDisabled(event) {
    wx.showToast({
      title: `标签 ${event.detail.index + 1} 已被禁用`,
      icon: 'none'
    });
  },
  onClickNavRight() {
    wx.showToast({
      title: '点击 right nav',
      icon: 'none'
    });
  },
  onClick(event) {
    wx.showToast({
      title: `点击标签 ${event.detail.index + 1}`,
      icon: 'none'
    });
  },
  // taskFinish: function () {
  //   wx.request({
  //     url: app.globalData.urlHome + '/task/finishTask',
  //     header: {
  //       "accept": "*/*",
  //       "content-type": "application/json",
  //       "Authorization": app.globalData.token
  //     },
  //     data: {
  //       "openid": app.globalData.openid,
  //       "type": 2
  //     },
  //     method: 'POST',
  //     success(r) {
  //       console.log(r.data);
  //     },
  //     fail(e) {
  //     }
  //   })
  // },

  cmp_dis:function(a,b){
    return a.distance-b.distance;
  },

  cmp_hot:function(a,b){
    return b.hot-a.hot;
  },

  disSort:function(){
    var pics=this.data.pics;
    pics.sort(this.cmp_dis);
    //console.log(pics);
    this.setData({
      pics:pics,
      nowsort:1
    })
  },

  hotSort:function(){
    var pics=this.data.pics;
    pics.sort(this.cmp_hot);
    //console.log(pics);
    this.setData({
      pics:pics,
      nowsort:1
    })
  },

  Mysort(e){
    console.log('mysort');
    var title=e.detail.title;

    if(title=='最近'){
      this.disSort();
    }else if(title=='最热'){
      this.hotSort();
    }
  },

  chooseBydis(e){
    var json=e.currentTarget.dataset;
    var left=json.left;
    var right=json.right;

    var pics=this.data.originPics;
    var res=[];
    for(var i=0;i<pics.length;i++){
      if(pics[i].distance>=left && pics[i].distance<=right){
        res.push(pics[i]);
      }
    }

    this.setData({
      pics:res
    })

    if(this.data.nowsort==0){
      this.disSort();
    }else if(this.data.nowsort==1){
      this.hotSort();
    }
  },

  chooseByClass(e){
    var type=e.currentTarget.dataset.type;
    var pics=this.data.originPics;
    var res=[];
    for(var i=0;i<pics.length;i++){
      if(pics[i].classify==type || type=="all"){
        res.push(pics[i]);
      }
    }
    this.setData({
      pics:res
    })
    if(this.data.nowsort==0){
      this.disSort();
    }else if(this.data.nowsort==1){
      this.hotSort();
    }
  }
})
